#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    char *inFile = argv[1];
    char *outFile = "./out.dat";
	int inFd = -1, outFd = -1;
	printf("if = '%s' \n", inFile);

	inFd = open(inFile, O_RDONLY);
	outFd = open(outFile, O_CREAT|O_RDWR, 00666);
	assert((inFd > 0)  && (outFd > 0));
	printf("in(%d), out(%d) \n", inFd, outFd);
	//ssize_t read(int fd, void *buf, size_t count);
	int num = 0;
	ssize_t ret;
	char buf[1];

	while (1)
	{

	   	ret = read(inFd, buf, 1);
		if (ret != 1)
		{
			printf("eos... \n");
			break;
		}

		num++;
		if (num&0x1)
		{
		    ret = write(outFd, buf, 1);
			if (ret != 1)
			{
				printf(" error, '%d' fd:'%d'\n", errno, outFd);
				break;
			}

		}

	}	
	close(outFd);
	close(inFd);
	sync();
	return 0;
}
